from imports import *

class Mario(pygame.sprite.Sprite):
##    from imports import *
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
#        self.imagename = 'Mariostand.png'
#        self.imagename = 'small/_mariostand.png' # using to get under blocks
        self.imagename = 'small/Mariostand.png' # had problem with other sprite
        self.image = image_load(self.imagename)
        self.rect = self.image.get_rect()
        self.rect.centerx = 300
        self.rect.bottom = 400
        self.blocks = []
        self.walkorstand = 0
        self.jumping = False
        self.jumptime = 0
        self.Graceperiod = False
        self.grounded = True
        self.dead = False
        self.screenpos = 0
        self.movingright = False
        self.movingleft = False
        self.facing = "right"
        self.dy = 0
        self.ychange = 0
        self.xchange = 0
    def gravity(self):
        return .05 * self.dy ** 2 - 2
    def update(self):
##    def update(self, BLOCKS):
        self.xchange = 0
        self.ychange = 0
        key = pygame.key.get_pressed()
        if key[K_UP]:
            if not self.jumping:
                self.jumping = True
                self.jumptime = 17
        if key[K_LEFT]:
            self.movingleft = True
            if self.rect.bottom <= 400:
                #self.rect.centerx -= 10
                self.xchange += -10
            if self.facing != 'left':
                self.image = pygame.transform.flip(self.image, 1, 0)
                self.facing = 'left'
            self.walkorstand += 1
            if self.walkorstand >= 5:
                if 'walk' in self.imagename:
                    self.imagename = self.imagename.rstrip('walk.png')+'stand.png'
                elif 'stand' in self.imagename:
                    self.imagename = self.imagename.rstrip('stand.png')+'walk.png'
                self.image = image_load(self.imagename)
                self.image = pygame.transform.flip(self.image, 1, 0)
                self.walkorstand = 0
        if key[K_RIGHT]:
            self.movingright = True
            if self.rect.bottom <= 400:
                #self.rect.centerx += 10
                self.xchange += 10
            self.walkorstand += 1
            if self.facing != 'right':
                self.image = pygame.transform.flip(self.image, 1, 0)
                self.facing = 'right'
            if self.walkorstand >= 5:
                if 'walk' in self.imagename:
                    self.imagename = self.imagename.rstrip('walk.png')+'stand.png'
                elif 'stand' in self.imagename:
                    self.imagename = self.imagename.rstrip('stand.png')+'walk.png'
                self.image = image_load(self.imagename)
                self.walkorstand = 0
        if key[K_k]:
            print("Hit Kill button @ (" + str(self.rect.left) + ", " + str(self.rect.top) + ")")
            self.rect.top = 1000
        if key[K_ESCAPE]:
            exit()
        keyempty = True
        if key[K_UP] or key[K_LEFT] or key[K_RIGHT]:
            keyempty = False
        if keyempty:
            if self.imagename.endswith('walk.png'):
                self.imagename = self.imagename.rstrip('walk.png') + 'stand.png'
            self.image = image_load(self.imagename)
            if self.facing == 'left':
                self.image = pygame.transform.flip(self.image, 1, 0)
        if self.jumping:
            self.Graceperiod = True
            if self.jumptime > 0:
                #self.rect.centery -= self.jumptime
                self.ychange -= self.jumptime
                self.jumptime -= 1
            if self.jumptime <= 0:
                self.jumptime = 0
                if self.grounded:
                    self.jumping = False
                self.Graceperiod = False
        # OK to let Mario jump above the top of the screen
        #if self.rect.top < 0:
            #self.rect.top = 0
            #self.Graceperiod = False
            #self.jumptime = 0
        if self.rect.left < 0:
            self.rect.left = 0
        if self.rect.right > 1000:
            self.rect.right = 1000
       
        self.onblock = False
##        for block in BLOCKS:
        for block in self.blocks:
            if block.rect.colliderect(self.rect):
                # what happens if Mario manages to get inside a block
                self.onblock = True
                top, right, bottom, left, inside = collide_edges(block.rect, self.rect)
                if top and not bottom:
                    self.rect.bottom = block.rect.top
                    self.grounded = True
                    self.dy = 0
                    self.jumping = False
                elif bottom and not top:
                    self.rect.top = block.rect.bottom
                    self.Graceperiod = False
                    self.jumptime = 0
                elif left:
                    #self.rect.right = block.rect.left
                    self.xchange = self.rect.right - block.rect.left
                elif right:
                    #self.rect.left = block.rect.right
                    self.xchange = self.rect.left - block.rect.right
                elif inside:
                    if self.xchange > 0:
                        self.rect.right = block.rect.left # not a good way
                    elif self.xchange < 0:
                        self.rect.left = block.rect.right
                    elif self.ychange > 0:
                        self.rect.bottom = block.rect.top
                    elif self.ychange < 0:
                        self.rect.top = block.rect.bottom
        if not self.onblock:
            self.grounded = False
        if not self.grounded:
            if not self.Graceperiod:
                self.dy += 1
                if not self.onblock:
                    self.ychange += self.gravity()
        self.rect.centerx += self.xchange
        self.rect.centery += self.ychange
    def reset(self):
        self.screenpos = 0
        self.rect.centerx = 300
        self.rect.bottom = 400
        self.grounded = True
        self.dy = 0
        self.xchange = 0
        self.ychange = 0
